﻿using log4net;
using log4net.Appender;
using log4net.Layout;
using log4net.Repository.Hierarchy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utils.Common.Enum;

namespace Utils.Common
{
    public  class LogHelper
    {

       //OFF < FATAL < ERROR < WARN < INFO < DEBUG  < ALL

       //高于等级设定值方法（如何设置参见“配置文件详解”）都能写入日志， Off所有的写入方法都不写到日志里，ALL则相反。
        private static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger(LogEnum.Info.ToString());

        private static readonly log4net.ILog logerror = log4net.LogManager.GetLogger(LogEnum.Error.ToString());

        private static readonly log4net.ILog logdebug = log4net.LogManager.GetLogger(LogEnum.Debug.ToString());


        /// <summary>
        /// 运行日志
        /// </summary>
        public static EventHandler LogEventHandler;


        public static void WriteInfo(string mes)
        {

            if (loginfo.IsInfoEnabled)
            {
                loginfo.Info(mes);
            }
        }

        public static void WriteError(string info, Exception ex)
        {
            if (logerror.IsErrorEnabled)
            {
                logerror.Error(info, ex);
            }
        }


        public static void WriteError(string mes)
        {
            if (logerror.IsInfoEnabled)
            {
                logerror.Error(mes);
            }
        }

        public static void WriteDebug(string mes)
        {
            if (logdebug.IsInfoEnabled)
            {
                logdebug.Debug(mes);
            }
        }

        public class LogEntity
        {
            public LogEnum LogEnum { get; set; }
            public string Message { get; set; }

            public int WorkStation { get; set; }
        }


        /// <summary>
        /// 运行日志错误信息
        /// </summary>
        /// <param name="mes"></param>
        public static void LogError(string mes ,int wk=0)
        {
            if (LogEventHandler != null)
            {
                LogEntity logEntity = new LogEntity();
                logEntity.LogEnum = LogEnum.Error;
                logEntity.Message = mes;
                logEntity.WorkStation = wk;
                LogEventHandler(logEntity, null);
                LogHelper.WriteError(mes);
            }
        }
        /// <summary>
        /// 运行日志错误信息
        /// </summary>
        /// <param name="mes"></param>
        public static void LogError(string mes ,Exception ex, int wk = 0)
        {
            if (LogEventHandler != null)
            {
                LogEntity logEntity = new LogEntity();
                logEntity.LogEnum = LogEnum.Error;
                logEntity.Message = mes;
                logEntity.WorkStation = wk;
                LogEventHandler(logEntity, null);
                LogHelper.WriteError(mes,ex);
            }
        }

        /// <summary>
        /// 运行日志错误信息
        /// </summary>
        /// <param name="mes"></param>
        public static void LogError( Exception ex, int wk = 0)
        {
            if (LogEventHandler != null)
            {
                LogEntity logEntity = new LogEntity();
                logEntity.LogEnum = LogEnum.Error;
                logEntity.Message = ex.Message;
                logEntity.WorkStation = wk;
                LogEventHandler(logEntity, null);
                LogHelper.WriteError(ex.Message, ex);
            }
        }

        /// <summary>
        /// 运行日志 普通信息
        /// </summary>
        /// <param name="mes"></param>
        public static void LogInfo(string mes,int wk = 0)
        {
            if (LogEventHandler != null)
            {
                LogEntity logEntity = new LogEntity();
                logEntity.LogEnum = LogEnum.Info;
                logEntity.Message = mes;
                logEntity.WorkStation = wk;
                LogEventHandler(logEntity, null);
                LogHelper.WriteInfo(mes);
            }
        }


        /// <summary>
        /// 运行日志 程序信息
        /// </summary>
        /// <param name="mes"></param>
        public static void LogDebug(string mes, int wk = 0)
        {
    
            if (LogEventHandler != null)
            {
                LogEntity logEntity = new LogEntity();
                logEntity.LogEnum = LogEnum.Debug;
                logEntity.Message = mes;
                logEntity.WorkStation = wk;
                LogEventHandler(logEntity, null);
                LogHelper.WriteDebug(mes);
            }
        }

    }

}
        

     